/*
 * Copyright 2011 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.element.steuer.rollbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;

import de.mnit.basis.fehler.Fehler;
import de.mnit.basis.swt.element.steuer.rollbar.A_SWT_Rollbar;

public class SWT_Liste extends A_SWT_Rollbar<SWT_Liste,List> {

	private int multi=SWT.SINGLE;


	public SWT_Liste() {
		s1_Rahmen(true);
	}


	// FUNKTION

	public SWT_Liste s1_MehrfachAuswahl() {
		this.PN_Erzeugt();
		this.multi=SWT.MULTI;
		return this;
	}

	public SWT_Liste plus(String... text) {
		Fehler.objekt.wenn_Null((Object)text);
		for(String t : text)
			swt().add(t);
		return this;
	}

	public SWT_Liste plus(String text, int position) {
		Fehler.objekt.wenn_Null(text);
		Fehler.zahl.wenn_Ausserhalb(0, gAnzahl(), position); // TODO Prüfen!
		swt().add(text,position);
		return this;
	}

	public SWT_Liste minus(int position) {
		Fehler.zahl.wenn_Ausserhalb(1,gAnzahl(),position);
		swt().remove(position-1);
		return this;
	}

	public SWT_Liste minus(int[] position) {
		Fehler.objekt.wenn_Null(position);
		for(int i=0; i<position.length; i++) {
			Fehler.zahl.wenn_Ausserhalb(1,gAnzahl(),position[i]);
			position[i]=position[i]-1;
		}
		swt().remove(position);
		return this;
	}

	public SWT_Liste minus(int pos_von, int pos_bis) {
		Fehler.zahl.wenn_Ausserhalb(1,pos_bis-1,pos_von);
		Fehler.zahl.wenn_Ausserhalb(pos_von+1,gAnzahl(),pos_bis);
		swt().remove(pos_von-1,pos_bis-1);
		return this;
	}

	public SWT_Liste leeren() {
		swt().removeAll();
		return this;
	}

	public SWT_Liste sAuswahl(String[] auswahl) {
		if(auswahl==null)
			swt().setSelection(-1);
		else
			swt().setSelection(auswahl);
		return this;
	}

	// LESEN

	public String gEintrag(int position) {
		return swt().getItem(position-1);
	}

	public String[] gEintraege() {
		return swt().getItems();
	}

	public int gAnzahl() {
		return swt().getItemCount();
	}

	public int gErstePosition(String text) {
		return swt().indexOf(text)+1;
	}

	public int gNaechstePosition(String text, int start) {
		return swt().indexOf(text,start-1)+1;
	}

	public int gAuswahlPosition() {
		return swt().getSelectionIndex()+1;
	}

	public int[] gAuswahlPositionen() {
		int[] ia=swt().getSelectionIndices();
		for(int i=0; i<ia.length; i++)
			ia[i]=ia[i]+1; return ia;
	}

	public String[] gAuswahl() {
		return swt().getSelection();
	}

	public int gAuswahlAnzahl() {
		return swt().getSelectionCount();
	}

	// ERZEUGEN

	protected int style4() {
		return this.multi;
	}

	protected List roh(Composite basis, int style) {
		return new List(basis,style);
	}

	protected void init4() {}

}
